<template>
  <section>
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item class="mb-10">
          <el-input size="small" v-model="filters.name" placeholder="参数名称"></el-input>
        </el-form-item>
        <el-form-item class="mb-10">
          <el-button size="small" type="danger" v-on:click="getConfs">搜索</el-button>
        </el-form-item>
        <el-form-item class="mb-10">
          <el-button size="small" type="success" @click="handleAdd">新建参数</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <el-table :data="confData" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
      style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label='序号' min-width="60">
      </el-table-column>
      <el-table-column prop="title" label="参数名称" min-width="120" sortable>
      </el-table-column>
      <el-table-column prop="title" label="参数描述" min-width="120" sortable>
      </el-table-column>
      <el-table-column prop="k" label="key" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="" label="坐标起点" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="" label="长度" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="a" label="系数" min-width="100" sortable>
      </el-table-column>
      <!--      <el-table-column prop="b" label="倍数" min-width="100" sortable>
      </el-table-column> -->
      <el-table-column prop="" label="已关联型号" min-width="120" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--    工具条
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total"
        style="float:right;">
      </el-pagination>
    </el-col> -->

    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="描述">
          <el-input v-model="editForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="系数">
          <el-input v-model="editForm.a" auto-complete="off"></el-input>
        </el-form-item>
        <!--        <el-form-item label="倍数">
          <el-input v-model="editForm.b" auto-complete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="key">
          <el-input v-model="editForm.k" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="success" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增" :visible.sync='addFormVisible' :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="描述">
          <el-input v-model="addForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="系数">
          <el-input v-model="addForm.a" auto-complete="off"></el-input>
        </el-form-item>
        <!--       <el-form-item label="倍数">
          <el-input v-model="addForm.b" auto-complete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="key">
          <el-input v-model="addForm.k" auto-complete="off"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="success" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>


    <div class="block">
      <el-pagination style="text-align: right;" background layout="total,prev, pager, next"
        :current-page="pageNo" @current-change="handleCurrentChange" :page-size="pageSize" :total="total"></el-pagination>
    </div>

  </section>

</template>
<script>
  export default {
    name: 'test6',
    //组件创建
    created() {},
    mounted() {
      this.getConfs();
    },
    data() {
      return {
        filters: {
          name: ''
        },
        confData: [],
        total: 0,
        page: 1,
        listLoading: false,
        sels: [], //列表选中列
        pageNo: 1,
        pageSize: 10,
        editFormVisible: false, //编辑界面是否显示
        editLoading: false,
        editFormRules: {
          name: [{
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }]
        },
        //编辑界面数据
        editForm: {


        },

        addFormVisible: false, //新增界面是否显示
        addLoading: false,
        addFormRules: {
          name: [{
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }]
        },
        //新增界面数据
        addForm: {
          title: '',
          a: '',
          b: '',
          k: ''
        }

      }
    },
    methods: {

      //获取参数列表
      getConfs() {

        var params = {

          pageNo: this.pageNo,
          pageSize: this.pageSize,

        }

        this.api.get('/work/awbConfItem/list', {params: params}).then((res) => {
          this.confData = res.data.result.records;
          this.total = res.data.result.total
        });
      },
      handleCurrentChange(val) {
        this.pageNo = val;
        this.getConfs();
      },
      //删除
      handleDel: function(index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          var params = {

            id: row.id
          }
          this.api.post('/work/awbConfItem/delete', params).then((res) => {
            this.listLoading = false;
            this.$message.success('操作成功');
            this.getConfs();
          });
        }).catch(() => {

        });
      },
      //显示编辑界面
      handleEdit: function(index, row) {
        this.editFormVisible = true;
        this.editForm = Object.assign({}, row);

      },
      //显示新增界面
      handleAdd: function() {
        this.addFormVisible = true;

      },
      //编辑
      editSubmit: function() {
        this.$refs.editForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              var params = {

                a: this.editForm.a,
                b: this.editForm.b,
                k: this.editForm.k,
                title: this.editForm.title,
                id: this.editForm.id
              }
              this.api.post('/work/awbConfItem/edit', params).then((res) => {
                if(res){
                  this.editFormVisible = false;
                  this.$message.success('操作成功');
                  this.getConfs();
                }
                this.editLoading = false;
              });

            });
          }
        });
      },
      //新增
      addSubmit: function() {
        this.$refs.addForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.addLoading = true;
              var params = {

                a: this.addForm.a,
                b: this.addForm.b,
                key: this.addForm.k,
                title: this.addForm.title,

              }
              this.api.post('/work/awbConfItem/add', params).then((res) => {
                if(res){
                  this.addFormVisible = false;
                  this.$message.success('操作成功');
                  // this.getAllFactory();
                  this.getConfs();
                }
                this.addLoading = false;
              });

            });
          }
        });
      },
      selsChange: function(sels) {
        this.sels = sels;
      },
      //批量删除
      batchRemove: function() {
        var ids = this.sels.map(item => item.id).toString();
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          let para = {
            ids: ids
          };
          batchRemoveUser(para).then((res) => {
            this.listLoading = false;
            //NProgress.done();
            this.$message.success('操作成功');
            this.getConfs();
          });
        }).catch(() => {

        });
      }
    }
  }
</script>
<style lang="less" scoped>
</style>
